<template>
  <el-card>
    <div slot="header" class="clearfix">
      <span>开票审核记录</span>
    </div>

    <!--<div class="clearfix mb15">-->
      <v-searchForm
        :form-data="filter_info"
        :form-title="formTitle"
        @search="search"
        @reset="reset"/>
    <!--</div>-->
    <v-tableBtns
      :table-title="exportTitle"
      :table-data="exportData"
      :on-export="handleExport"
      title="开票资料"
    />

    <v-table
      :table-title="tableTitle"
      :table-data="tableData"
      :readable="true"
      :editable="false"
      :deletable="admin||assistantRole||financeRole"
      btn-class="btn"
      @sortChange="sortChange"
      @del="onDelete"
      @edit="edit"
      @read="read"
    />

    <v-pagination
      :page-info="pageInfo"
      :get-table-list="getTableList"
    />
  </el-card>
</template>

<script>
  import {mapGetters} from 'vuex'
  import auth from '@/mixins/auth'

  export default {
    name: 'Home',
    mixins: [auth],
    data() {
      return {
        // f分页
        pageInfo: {
          pageNumber: 1,
          pageSize: 30,
          totalPage: 0,
          totalRow: 0
        },
        // 排序
        sidx: 'reqid',
        sortKey: null,

        formTitle: [
          {
            cn: 'id',
            en: 'req_id'
          },
          // {
          //   cn: '合同编号: ',
          //   en: 'contractCode'
          // },
          {
            cn: '企业名称: ',
            en: 'company_name',
          },
          {
            cn: '负责人查询: ',
            en: 'staffName',
            placeholder: '商务名/助理名'
          },
          {
            cn: '开票金额: ',
            en: 'money',
            type: 'range',
            dataName: [{min: 'moneyMin', max: 'moneyMax'}]
          },
          {
            cn: '申请时间: ',
            en: 'created_at',
            type: 'time'
          }
        ],
        filter_info: {},
        exportTitle: [
          {
            cn: '创建日期',
            en: 'create_date',
          },
          {
            cn: '企业名称',
            en: '开票公司名称',
          },
          {
            cn: '税号',
            en: '纳税人识别号',
          },
          {
            cn: '公司地址',
            en: '公司地址',
          },
          {
            cn: '电话',
            en: '电话',
          },
          {
            cn: '开户行',
            en: '开户行',
          },
          {
            cn: '帐号',
            en: '帐号',
          },
          {
            cn: '开票金额',
            en: 'money',
          },
          {
            cn: '开票内容',
            en: 'invoice_content',
          },
          {
            cn: '发票类别',
            en: '发票类别',
          },
          {
            cn: '商务',
            en: 'swname',
          },
          {
            cn: '对应订单',
            en: 'mpname',
          },
          {
            cn: '备注',
            en: 'remark',
          },
          {
            cn: '开票状态',
            en: 'cur_status',
          },
        ],
        exportData: [],

        tableTitle: [
          {
            cn: 'id',
            en: 'reqid',
            sortable: true
          },
          // {
          //   cn: '合同编号',
          //   en: 'contract_num',
          //   sortable: true
          // },
          {
            cn: '企业名称',
            en: 'company_name',
            sortable: true
          },
          {
            cn: '税号',
            en: '纳税人识别号'
          },
          {
            cn: '发票类别',
            en: 'invoice_type',
            sortable: true
          },
          {
            cn: '开票金额',
            en: 'money',
            sortable: true
          },
          {
            cn: '商务',
            en: 'swname',
            sortable: true
          },
          {
            cn: '申请人',
            en: 'create_name',
            sortable: true
          },
          {
            cn: '申请时间',
            en: 'create_date',
            sortable: true
          },
          {
            cn: '状态',
            en: 'cur_status',
            sortable: true
          }
        ],
        tableData: []

      }
    },
    activated() {
      this.getTableList()
    },
    methods: {

      getTableList(data) {
        const loading = this.$loading()

        const datas = {
          ...this.filter_infos,
          checkState: 7,
          page: this.pageInfo.pageNumber, rows: this.pageInfo.pageSize,
          sidx: this.sidx,
          sortKey: this.sortKey,
        }
        this.$get('action/InvoiceRequisition/dataGrid', datas)
          .then(res => {
            const list = res.data.list.map(item => {
              switch (item.cur_status) {
                case 7:
                  item.cur_status = '已完成'
                  break
                default :
                  item.cur_status = '商务填写'
                  break
              }
              switch (item.invoice_type) {
                case '1':
                  item.invoice_type = '专票'
                  break
                case '2':
                  item.invoice_type = '普票'
                  break
                default :
                  item.invoice_type = '普票'
                  break
              }
              return item
            })
            this.pageInfo.totalPage = res.data.totalPage
            this.pageInfo.totalRow = res.data.totalRow
            this.tableData = list || []
              loading.close()
          })
          .catch(err => {
            loading.close()
            this.$message({
              message: '获取数据失败',
              type: 'error'
            })
          })
      },

      addNew() {
        // 跳转到创建页面
        this.$router.push({name: 'invoice_manage.application'})
      },
      // 搜索
      search() {
        this.pageInfo.pageNumber=1
        this.getTableList()
      },
      reset() {
        this.filter_info = {}
        this.getTableList()
      },
      edit(val) {
        this.$router.push({
          name: 'invoice_manage.application', query: {
            id: val.row.reqid
          }
        })
      },
      read(val) {
        this.$router.push({
          name: 'invoice_manage.preview',
          query: {
            id: val.row.reqid
          }
        })
      },
      handleExport() {
        this.$get('action/InvoiceRequisition/GetFile', {type: 0,checkState: 7,  sidx: 'id', ...this.filter_infos})
          .then(res => {
            let datas = res.data;
            let list = datas.map((item) => {
              const obj = item
              switch (obj.cur_status) {
                case  3:
                  obj.cur_status =  '财务退回';
                  break
                case  4:
                  obj.cur_status =  '财务审核';
                  break
                case  5:
                  obj.cur_status =  '开票中';
                  break
                case  6:
                  obj.cur_status =  '待领取';
                  break
                case  7:
                  obj.cur_status =  '已完成';
                  break
                default :
                  break
              }
              switch (obj.invoice_content) {
                case  '1':
                  obj.invoice_content =  '广告费';
                  break
                case  '2':
                  obj.invoice_content =  '广告发布费';
                  break
                case  '3':
                  obj.invoice_content =  '信息服务费';
                  break
                case  '4':
                  obj.invoice_content =  '信息技术服务费';
                  break
                case  '5':
                  obj.invoice_content =  '服务费';
                  break
                default :
                  break
              }
              switch (obj.发票类别) {
                case  '1':
                  obj.发票类别 =  '专票';
                  break
                case  '2':
                  obj.发票类别 =  '普票';
                  break
                default :
                  break
              }
              return obj
            })
            this.exportData = list || []

          }).catch(_ => {
          this.$message({
            message: '导出超时',
            type: 'error'
          })
          this.exportData = []

        })
      },
      onDelete(val) {
        let data
        data = {reqid: val.row.reqid}
        this.$post('action/InvoiceRequisition/del', data)
          .then(res => {
            this.getTableList()
            this.$message({
              message: res.msg,
              type: 'success'
            })
          })
      },
      // 排序
      sortChange({column, prop, order}) {
        console.log(column, prop, order)
        this.sidx = prop || 'reqid'
        this.sortKey = order === 'ascending' ? 'asc' : null
        this.getTableList()
      },

    },
    computed: {
      ...mapGetters([
        'name',
        'roles'
      ]),
      filter_infos() {
        const data = {}
        const origin = this.filter_info
        for (const key in origin) {
          if (!this.filter_info[key] && this.filter_info[key] !== 0) {
            this.$delete(this.filter_info, key)
            continue
          }
          if (key === 'created_at') {
            data.start_time = origin[key][0]
            data.End_time = origin[key][1]
          } else {
            data[key] = origin[key]
          }
        }
        if (!origin.created_at) {
          delete data.start_time
          delete data.End_time
        }
        return data
      }
    }
  }
</script>

